import dictDataLoader from "./dictDataLoader";


var md = {
    dictData: null,
    data() {
        // console.log(this.dictData);
        if (this.dictData == null) {
            this.dictData = [];
            var th = this;
            dictDataLoader.getData('dict', th, 'dictData', (data) => {
                this.dictData = data.content;

            });

        }
        return this.dictData;
    },
    /**
     *初始化字典项
     * @param callback   等待获取字典项后才能执行的函数
     */
    initDictData(callback) {
        // console.log(this.dictData);
        //如果未缓存字典，则调用接口查询字典项并缓存，再执行回调
        if (this.dictData == null) {
            this.dictData = [];
            var th = this;
            dictDataLoader.getData('dict', th, 'dictData', (data) => {
                this.dictData = data.content;

                callback();
            });
        } else {
            //已缓存字典，则直接执行回调
            callback();
        }
    },
    getDictName(pCode, dictCode) {
        this.dataList = this.data();
        for (var idx in this.dataList) {
            var item = this.dataList[idx];
            if (item.dictPcode != pCode) {
                continue;
            }
            if (item.dictCode == dictCode) {
                return item.dictName;
            }
        }
        return dictCode;
    },
    getDictNameForSel(pCode, parentCode) {
        let dataList = this.data();
        let rtDataList = [];
        for (var i in dataList) {
            var item = dataList[i];
            if (item.dictPcode == pCode && item.parentCode == parentCode) {
                rtDataList.push(item);
            }
        }
        return rtDataList;
    },
    /**
     *  根据dictPcode获取字典项列表
     */
    listDict(pCode) {
        let dataList = this.data();
        let rtDataList = [];
        for (var i in dataList) {
            var item = dataList[i];
            if (item.dictPcode == pCode) {
                rtDataList.push(item);
            }
        }
        return rtDataList;
    },

}


export default md;